// import { getMoreReply } from '../../../api/index'
import * as Api from '../../../api/index'
import { formatTimeTwo } from '../../../utils/util'

Component({
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    item: {
      type: Object,
      value: {}
    },
    apiName: {
      type: String,
      value: ''
    },
    commentApiName: {
      type: String,
      value: ''
    },
    key: {
      type: String,
      value: ''
    }
  },
  data: {
    commentList: [],
    showModal: false,
    currentItem: null,
    commentValue: ''
  },
  methods: {
    touchmove () {},
    replyComment () {
      this.triggerEvent('replyComment', this.data.item)
    },
    beforeOpen () {
      this.getList()
    },
    close () {
      this.triggerEvent('close')
    },
    reply ({ currentTarget: { dataset: { item } } }) {
      this.setData({
        currentItem: item,
        showModal: true
      })
    },
    moveHandle () {},
    changeInput ({ detail: { value } }) {
      this.setData({
        commentValue: value
      })
    },
    async commentConfirm () {
      try {
        let query = {
          commentContext: this.data.commentValue
        }
        query[this.data.key] = this.data.item[this.data.key]
        if (this.data.item) {
          query.parentId = this.data.item.id
          query.topId = this.data.item.topId
        }
        await Api[this.data.commentApiName]({...query})
        wx.showToast({ title: '评论成功！', icon: 'none' })
        this.setData({
          currentItem: null,
          commentValue: ''
        })
        this.getList()
      } catch (err) {
        console.log(err)
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'nonoe' })
      }
    },
    async send ({ detail }) {
      try {
        let query = {
          commentContext: detail
        }
        query[this.data.key] = this.data.item[this.data.key]
        if (this.data.currentItem) {
          query.parentId = this.data.currentItem.id
          query.topId = this.data.currentItem.topId
        }
        await Api[this.data.commentApiName]({...query})
        wx.showToast({ title: '评论成功！', icon: 'none' })
        this.setData({
          showModal: false,
          currentItem: null
        })
        this.getList()
      } catch (err) {
        console.log(err)
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'nonoe' })
      }
    },
    closeModal () {
      this.setData({
        showModal: false,
        currentItem: null
      })
    },
    async getList () {
      try {
        let query = { topId: this.data.item.id }
        query[this.data.key] = this.data.item[this.data.key]
        const { ResultSet } = await Api[this.data.apiName]({ ...query })
        ResultSet.userData.data.forEach(item => {
          item.commentTime = formatTimeTwo(item.commentTime * 1000, 'Y-M-D h:m')
        })
        this.setData({ commentList: ResultSet.userData.data })
      } catch (err) {
        console.log(err)
        wx.showToast({ title: err.message || err.ResultSet.errors, icon: 'nonoe' })
      }
    }
  },
})